package day06Case01;

import java.util.Random;
import java.util.Scanner;

/**
 * @author Xiao
 * @date 2021/7/6
 */
public class Print_Array {
    /**
     *      需求：
     *          打印任意int类型数组的所有元素
     */
    public static void main(String[] args) {
        int[] arr = getAnyArray();
        System.out.println("产生的随机数组为：");
        printArray(arr);
    }

    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                System.out.print(arr[i]);
            } else {
                System.out.print(arr[i] + ", ");
            }
        }
        System.out.println("]");
    }

    public static int[] getAnyArray() {
        Scanner sc = new Scanner(System.in);
        Random r = new Random();        int length = 0;
        while (true) {
            System.out.println("请输入一个整数作为数组长度(5-10)：");
            int num = sc.nextInt();
            if (num >= 5 && num <= 10) {
                length = num;
                break;
            } else {
                System.out.println("您输入的数组长度有误。");
            }
        }
        int[] arr = new int[length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt(10);
        }
        return arr;
    }
}
